﻿using Anata_Client;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication2.Model;

namespace WindowsFormsApplication2
{
    public partial class Form5 : Form
    {
        ArrayList transaksiLayanan;
        ArrayList transaksiProduk;

        public Form5()
        {
            InitializeComponent();
        }

        public Form5(ref ArrayList a, ref ArrayList b)
        {
            InitializeComponent();

            transaksiLayanan = a;
            transaksiProduk = b;

            ArrayList k = Karyawan.DaftarKaryawanIdNama(Form1.idCabang);
            int n = jumlahKolom(k);
            object[] o = new object[2];

            dgvSchedule.Columns.Add("Id", "Id");
            dgvSchedule.Columns.Add("Nama", "Nama");

            for (int i = 0; i < n; ++i)
            {
                dgvSchedule.Columns.Add("Task " + i.ToString(), "Task " + i.ToString());
            }

            for (int i = 0; i < k.Count; ++i)
            {
                o = (object[])k[i];
                o = isiBaris((int)o[0], (string)o[1]);
                dgvSchedule.Rows.Add(o);

                DataGridViewRow dr = dgvSchedule.Rows[dgvSchedule.Rows.Count-1];
                for (int j = 0; j < o.Length-2; ++j)
                    dr.Cells[2+j].Style.BackColor = Color.Red;
            }
        }

        private int jumlahKolom(ArrayList k)
        {
            int m = 0, n = 0;
            object[] o, p;

            for (int j = 0; j < k.Count; ++j)
            {
                m = 0;
                o = (object[])k[j];
                for (int i = 0; i < transaksiLayanan.Count; ++i)
                {
                    p = (object[])transaksiLayanan[i];
                    if ((string)o[1] == (string)p[4])
                    {
                        ++m;
                    }
                }
                if (m > n)
                    n = m;
            }

            return n;
        }

        private object[] isiBaris(int _id, string _nama)
        {
            ArrayList a = new ArrayList();
            object[] o = new object[1];

            for (int i = 0; i < transaksiLayanan.Count; ++i)
            {
                o = (object[])transaksiLayanan[i];
                if ((string)o[4] == _nama)
                    a.Add((string)o[5] + " (" + o[1].ToString() + ")");
            }

            o = new object[a.Count+2];
            o[0] = _id;
            o[1] = _nama;
            for (int i = 0; i < a.Count; ++i)
            {
                o[i+2] = a[i];
            }

            return o;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btnMulai_Click(object sender, EventArgs e)
        {
            DataGridViewCell dc = dgvSchedule.CurrentCell;

            if (dc.Style.BackColor == Color.Red)
                dc.Style.BackColor = Color.Blue;
            else if (dc.Style.BackColor == Color.Blue)
                dc.Style.BackColor = Color.Red;

            dc.Selected = false;
        }

        private void btnAlokasi_Click(object sender, EventArgs e)
        {
            char[] sep = new char[2];
            sep[0] = '('; sep[1] = ')';
            DataGridViewCell dc = dgvSchedule.CurrentCell;
            int idTransaksi = Convert.ToInt32(dc.Value.ToString().Split(sep)[1]);

            frmAlokasi f = new frmAlokasi(ref transaksiLayanan, ref transaksiProduk, idTransaksi);
            f.Show();
            this.Close();
        }
    }
}
